package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 分类管理
 */
@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "category",allEntries = true)
    public R<String> addCategory(@RequestBody Category category){
//        Set keys = redisTemplate.keys("category*");
//        redisTemplate.delete(keys);
        categoryService.save(category);
        //延迟双删
//        try {
//            Thread.sleep(100);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        Set keys2 = redisTemplate.keys("category*");
//        redisTemplate.delete(keys2);
        return R.success("success");
    }
    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("page")
    public R<Page<Category>> selectByPage(Integer page,Integer pageSize){
        Page<Category> newPage = new Page<>(page,pageSize);
        Page<Category> page1 = categoryService.lambdaQuery()
                .orderByAsc(Category::getSort).page(newPage);
        return R.success(page1);
    }

    /**
     * 修改分类信息
     * @param category
     * @return
     */
    @PutMapping
    @CacheEvict(cacheNames = "category",allEntries = true)
    public R<String> update(@RequestBody Category category){
//        Set keys = redisTemplate.keys("category*");
//        redisTemplate.delete(keys);
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "category",allEntries = true)
    public R<String> delete(Long id){
//        Set keys = redisTemplate.keys("category*");
//        redisTemplate.delete(keys);
        categoryService.deleteById(id);
        return R.success("删除成功");
    }

    /**
     * 根据条件 查询所有分类信息
     * @param type
     * @return
     */
    @GetMapping("list")
    @Cacheable(cacheNames = "category",key = "''+#type")
    public R<List<Category>> categoryType(Integer type){
//        String redisKey = "category_list_"+type;
//        String key = "category_"+DigestUtils.md5Hex(redisKey);
//        List<Category> category_list = (List<Category>)redisTemplate.opsForValue().get(key);
//        if(category_list!=null){
//            return R.success(category_list);
//        }
        List<Category> list = categoryService.lambdaQuery()
                .eq(type!=null,Category::getType, type)
                .orderByAsc(Category::getSort)
                .list();
//        redisTemplate.opsForValue().set(key,list);
        return R.success(list);
    }


}
